<?php


namespace app\api\controller\v32;


use app\aliyun\controller\AliyunFilterWord;
use app\api\controller\v2\Auth;
use app\common\cachemodel\ArticleComment;
use app\common\cachemodel\ArticleCommentReply;
use app\common\cachemodel\Goods;
use app\common\cachemodel\ZhunleActivity;
use app\common\cachemodel\zlcr2\Zlcr2QuestionUserView;
use app\common\cachemodel\zlcr2\Zlcr2UserAddQuestion;
use app\common\cachemodel\zlcr2\Zlcr2UserQuestion;
use app\common\cachemodel\zlcr2\Zlcr2UserQuestionComment;
use app\common\cachemodel\zlcr2\Zlcr2UserSameQuestion;
use app\common\cachemodel\ZlcrAppeal;
use app\common\cachemodel\ZlcrAstr;
use app\common\cachemodel\ZlcrAstrMessage;
use app\common\cachemodel\ZlcrTag;
use app\common\cachemodel\ZlUserVip;
use app\common\lib\SensorsData;
use app\common\lib\tarot\Tarot;
use app\common\lib\Util;
use app\common\model\Recode;
use app\common\model\User;
use app\common\model\UserPush;
use think\Cache;
use think\Env;
use think\Log;
use think\response\Json;
use XznsTools\cache\CacheManage;

class UserQuestionAnswer extends Auth
{
    /**
     * 问题-评论
     */
    public function questionComment()
    {
        $qid = input('qid/s', 0);
        $content = trim(htmlspecialchars(input('content/s', '')));

        if ($content === '')
        {
            return show(config('code.error'), '讨论内容不能为空');
        }
        if (!$qid)
        {
            return show(config('code.error'), '请求异常');
        }
        if ($this->user['no_comment'])
        {
            return show(config('code.error'), '发布讨论失败，已被禁止发布讨论');
        }
        $questionModel = new Zlcr2UserQuestion();
        $questionInfo = $questionModel->getInfoById($qid, ['uid', 'read_num', 'comment_num', 'same_num', 'id', 'consult_time', 'status', 'question_type', 'is_anonymity', 'is_hide', 'delete_time']);

        if (!$questionInfo)
        {
            return show(config('code.error'), '问题不存在');
        }
        if ($questionInfo['delete_time'] > 0)
        {
            return show(config('code.error'), '问题已禁止发布讨论');
        }
        $data = [
            'aid' => $qid,
            'uid' => (int)$this->user['id'],
            'content' => $content,
            'type' => 4,    //类型 1=>星文评论，2=>女神动态评论，3=>女神专区评论 4=>问答 必传
            'status' => 0   // 状态 0 正常 -1 删除
        ];
        $res = (new ArticleComment())->commentAdd($this->user, $data, false);

        if ($res['status'])
        {
            //发布讨论    QuestionCommentSuccess    打点
            SensorsData::track($this->user['id'], true, 'QuestionCommentSuccess', [
                'question_id' => $qid,
                'question_type' => $questionInfo['question_type'],
                'author_id' => $questionInfo['uid'],
                'comment_content' => $content
            ]);

            $anonymityNickname = '匿名用户';
            $anonymityAvatar = '/test/question_answer/anonymity1.png';

            $adorn = Util::getUserAdorn($this->user['id']);

            $rData = [
                'id' => (int)$res['id'],
                'avatar' => $questionInfo['uid'] === $this->user['id'] && $questionInfo['is_anonymity'] === 1 ? $anonymityAvatar : $this->user['avatar'],
                'avatar_adorn' => $questionInfo['uid'] === $this->user['id'] && $questionInfo['is_anonymity'] === 1 ? '' : $adorn['avatar_adorn'],
//                'badge' => $adorn['badge'],
                'nickname' => $questionInfo['uid'] === $this->user['id'] && $questionInfo['is_anonymity'] === 1 ? $anonymityNickname : $this->user['nickname'],
                'create_time' => '刚刚',
                'content' => htmlspecialchars_decode($content),
                'reply_num' => 0,
                'good_num' => 0,
                'is_good' => 0,
                'is_owner' => 1,
                'user_tag' => $questionInfo['uid'] === $this->user['id'] ? 1 : 0,    // 用户标签 0=>没有 1=>提问者 2=>楼主
                'reply_list' => [],
                'vip' => (new ZlUserVip())->getUserVipLevel($this->user['id'])
            ];

            $questionModel->changeQuestionInfo($questionInfo, 2, 1);

            if ($questionInfo['uid'] === $this->user['id'])
            {
                (new ArticleComment())->delQuestionAnswerRecommentIdCache($qid, 4);
            }

            return show(config('code.success'), '评论成功', $rData);
        }
        return show(config('code.error'), $res['msg']);

    }

    /**
     * 评论回复
     */
    public function commentReply()
    {
        $commentId = input('cid/d', 0);
        $pid = input('pid/d', 0);
        $content = trim(htmlspecialchars(input('param.content/s', '')));
        $commentModel = new ArticleComment();
        if ($content === '')
        {
            return show(config('code.error'), '评论内容不能为空');
        }
        if (!$commentId)
        {
            return show(config('code.error'), '讨论不存在');
        }
        if ($this->user['no_comment'])
        {
            return show(config('code.error'), '评论失败，已被禁止评论');
        }

        // 评论详情
        $commentInfo = current($commentModel->batchGetInfo([$commentId], ['uid', 'aid', 'goods', 'reply']));
        if (!$commentInfo)
        {
            return show(config('code.error'), '数据不存在');
        }
        // 判断 用户是否允许回复他
        if ($pid)
        {   // 回复->评论回复
            // 获取回复的 评论回复用户uid
            $replyUid = (new ArticleCommentReply())->getInfoById($pid, ['uid1']);
            if ($replyUid['uid1'] != $this->user['id'])
            {   // 不是回复自己
                $replyStatus = (new UserPush())->getUserReplyStatus((int)$replyUid['uid1']); // 是否允许回复自己 true 允许 false 不允许
                if (!$replyStatus)
                {   // 不允许回复
                    return show(config('code.error'), '对方关闭了评论回复');
                }
            }
        } else
        {   // 回复讨论
            // if ($commentInfo['uid'] != $this->user['id'])
            // {   // 不是自己评论自己事
            //     $replyStatus = (new UserPush())->getUserReplyStatus((int)$commentInfo['uid']); // 是否允许回复自己 true 允许 false 不允许
            //     if (!$replyStatus)
            //     {   // 不允许回复
            //         return show(config('code.error'), '对方关闭了评论回复');
            //     }
            // }
        }

        $data = [
            'comment_id' => $commentId,
            'pid' => $pid,
            'content' => $content,
            'uid' => (int)$this->user['id'],
            'status' => 0   // 状态 0 正常 -1 删除
        ];
        $question = (new Zlcr2UserQuestion())->getInfoById($commentInfo['aid'], ['uid', 'delete_time']);
        if ($question['delete_time'] > 0)
        {
            return show(config('code.error'), '问题关闭了评论回复');
        }

        $res = (new ArticleCommentReply())->addCommentReply($this->user, $data, false);

        if ($res['status'])
        {
            //  发布回复成功    QuestionReplySuccess    打点
            SensorsData::track($this->user['id'], true, 'QuestionReplySuccess', [
                'question_id' => $commentInfo['aid'],    // 是否匿名
                'author_id' => $question['uid'],// 提问者
                'owner_id' => $commentInfo['uid'],//楼主
                'reply_content' => $content,
            ]);

            $questionModel = new Zlcr2UserQuestion();
            $questionInfo = $questionModel->getInfoById($commentInfo['aid'], ['uid', 'is_anonymity']);
            if ($questionInfo['uid'] === $this->user['id'])
            {
                $userTag = 1;
            } elseif ((int)$commentInfo['uid'] === $this->user['id'])
            {
                $userTag = 2;
            }

            $anonymityNickname = '匿名用户';
            $anonymityAvatar = '/test/question_answer/anonymity1.png';

            $commentModel->setCommentAndGoodSum($commentInfo['aid'], $commentId, $commentInfo['goods'] + $commentInfo['reply'] + 1);

            $adorn = Util::getUserAdorn($this->user['id']);

            $rData = [
                'id' => (int)$res['id'],
                'avatar1' => $questionInfo['uid'] === $this->user['id'] && $questionInfo['is_anonymity'] === 1 ? $anonymityAvatar : $this->user['avatar'],
                'nickname1' => $questionInfo['uid'] === $this->user['id'] && $questionInfo['is_anonymity'] === 1 ? $anonymityNickname : $this->user['nickname'],
                'avatar_adorn1' => $questionInfo['uid'] === $this->user['id'] && $questionInfo['is_anonymity'] === 1 ? '' : $adorn['avatar_adorn'],
                'nickname2' => $pid ? $res['nickname2'] : null,
                'create_time' => '刚刚',
                'content' => htmlspecialchars_decode($content),
                'content_type' => 1,
                'good_num' => 0,
                'is_good' => 0,
                'is_owner' => 1,
                'user_tag' => $userTag ?? 0,    // 用户标签 0=>没有 1=>提问者 2=>楼主
                'vip' => (new ZlUserVip())->getUserVipLevel($this->user['id'])
            ];

            return show(config('code.success'), '评论成功', $rData);
        }
        return show(config('code.error'), $res['msg']);
    }

}
